﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using DotNetNuke.Modules.AjaxWebLinks.Components;
using System.Text.RegularExpressions;
using System.Threading;

namespace DotNetNuke.Modules.AjaxWebLinks
{
    /// <summary>
    /// Summary description for Handler
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [ScriptService]
    public class Handler : WebService
    {
        private AjaxWebLinksController controller;
        private AjaxSecurityContext securityContext;
        private const string URL_PATTERN = @"^https?://([\w]+\.)?[\w]+\.[\w/:.?=%]+$";
        private const string SCRIPT_PATTERN = @"";
        public Handler()
        {
            
            //Hook up security context
            securityContext = new AjaxSecurityContext(this.Context);
            //Get reference to business logic layer
            controller = new AjaxWebLinksController();
            //Then Have fun!!
           
        }


        [WebMethod]
        public IEnumerable<AjaxWebLinkInfo> GetLinks()
        {
            return controller.GetLinks(securityContext.moduleInfo.ModuleID);
        }

        [WebMethod]
        public AjaxWebLinkInfo GetLink(int LinkID)
        {
            return controller.GetLink(securityContext.moduleInfo.ModuleID, LinkID);
        }

        [WebMethod]
        public ResponseWrapper AddLink(string LinkURL, string LinkText)
        {
            var r = new ResponseWrapper();
            LinkURL = LinkURL.Trim();

            //Would be nice to create an attribute to assert permissions [access(level= accesslevel.edit)
            if (!securityContext.canEdit)
            {
                
                r.Error = "Permission Denied" ;
                return r;
            }

            var regex = new Regex(URL_PATTERN);

            if (!regex.IsMatch(LinkURL))
            {
                r.Error = "Invalid link format";
                return r;
            }

           // var scriptRegex = new Regex(SCRIPT_PATTERN);

            if (LinkText.Length == 0 )
            {
                r.Error = "Invalid Link text";
                return r;
            }

                //returns the id of the new link or 0 if failed
            
            var link = new AjaxWebLinkInfo();
            link.LinkText = LinkText;
            link.LinkURL = LinkURL;
            link.ModuleId = securityContext.moduleInfo.ModuleID;

            r.Data  = controller.AddLink(link);
            return r;

        }

        [WebMethod]
        public ResponseWrapper DeleteLink(int LinkID)
        {
            var r = new ResponseWrapper();
            
            if (!securityContext.canEdit)
            {
                r.Error = "Permission Denied";
                return r;
            }


            //returns the id of the new link or 0 if failed
            controller.DeleteLink(securityContext.moduleInfo.ModuleID, LinkID);

            r.Data = true;

            return r;
        }

        [WebMethod]
        public ResponseWrapper UpdateLink(int LinkID, string LinkText, string LinkURL)
        {
            var r = new ResponseWrapper();
            LinkURL = LinkURL.Trim();

            if (!securityContext.canEdit)
            {
                r.Error = "Permission Denied";
                return r;
            }

            var regex = new Regex(URL_PATTERN);

            if (!regex.IsMatch(LinkURL))
            {
                r.Error = "Invalid link format";
                return r;
            }

          //  var scriptRegex = new Regex(SCRIPT_PATTERN);

            if (LinkText.Length == 0)
            {
                r.Error = "Invalid Link text";
                return r;
            }

            var link = controller.GetLink(securityContext.moduleInfo.ModuleID, LinkID);
            if (link != null)
            {
                link.LinkText = LinkText;
                link.LinkURL = LinkURL;
                controller.UpdateLink(link);
                r.Data = true;
            }
            
            //TODO: add try catch and other logic to verify update took place

            return r;
        }


    }
}
